home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter8 / availdrv.c next >
C/C++ Source or Header  |  1996-04-28  |  8KB  |  258 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "availdrv.h"
  5. #include "sqlext.h"  
  6. #include "odbcinst.h"  
  7.  
  8.  
  9. #if defined (WIN32)
  10.     #define IS_WIN32 TRUE
  11. #else
  12.     #define IS_WIN32 FALSE
  13. #endif
  14.  
  15. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  16. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  17. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  18.  
  19. HINSTANCE hInst;   // current instance
  20.  
  21. LPCTSTR lpszAppName = "MyApp";
  22. LPCTSTR lpszTitle   = "SQLGetAvailableDrivers()"; 
  23.  
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26. VOID DisplayBuffer( HWND hList, LPCSTR lpszBuffer );
  27.  
  28.  
  29. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.                       LPTSTR lpCmdLine, int nCmdShow)
  31. {
  32.    MSG      msg;
  33.    HWND     hWnd; 
  34.    WNDCLASS wc;
  35.  
  36.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  37.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  38.    wc.cbClsExtra    = 0;                      
  39.    wc.cbWndExtra    = 0;                      
  40.    wc.hInstance     = hInstance;              
  41.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  42.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  43.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  44.    wc.lpszMenuName  = lpszAppName;              
  45.    wc.lpszClassName = lpszAppName;              
  46.  
  47.    if ( IS_WIN95 )
  48.    {
  49.       if ( !RegisterWin95( &wc ) )
  50.          return( FALSE );
  51.    }
  52.    else if ( !RegisterClass( &wc ) )
  53.       return( FALSE );
  54.  
  55.    hInst = hInstance; 
  56.  
  57.    hWnd = CreateWindow( lpszAppName, 
  58.                         lpszTitle,    
  59.                         WS_OVERLAPPEDWINDOW, 
  60.                         CW_USEDEFAULT, 0, 
  61.                         CW_USEDEFAULT, 0,  
  62.                         NULL,              
  63.                         NULL,              
  64.                         hInstance,         
  65.                         NULL               
  66.                       );
  67.  
  68.    if ( !hWnd ) 
  69.       return( FALSE );
  70.  
  71.    ShowWindow( hWnd, nCmdShow ); 
  72.    UpdateWindow( hWnd );         
  73.  
  74.    while( GetMessage( &msg, NULL, 0, 0) )   
  75.    {
  76.       TranslateMessage( &msg ); 
  77.       DispatchMessage( &msg );  
  78.    }
  79.  
  80.    return( msg.wParam ); 
  81. }
  82.  
  83.  
  84. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  85. {
  86.    WNDCLASSEX wcex;   
  87.  
  88.    wcex.style         = lpwc->style;
  89.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  90.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  91.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  92.    wcex.hInstance     = lpwc->hInstance;
  93.    wcex.hIcon         = lpwc->hIcon;
  94.    wcex.hCursor       = lpwc->hCursor;
  95.    wcex.hbrBackground = lpwc->hbrBackground;
  96.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  97.    wcex.lpszClassName = lpwc->lpszClassName;
  98.  
  99.    // Added elements for Windows 95.
  100.    //...............................
  101.    wcex.cbSize = sizeof(WNDCLASSEX);
  102.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  103.                             IMAGE_ICON, 16, 16,
  104.                             LR_DEFAULTCOLOR );
  105.             
  106.    return RegisterClassEx( &wcex );   
  107. }
  108.  
  109.  
  110. #define BUFFERSIZE 1024
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static HWND  hList       = NULL;
  115. static UCHAR szIniFile[] = "D:\\MSVC20\\REDIST\\ODBC.INF";
  116.  
  117.    switch( uMsg )
  118.    {
  119.       case WM_CREATE :
  120.               {
  121.                  hList = CreateWindow( "LISTBOX", "",    
  122.                                        LBS_NOTIFY | WS_VSCROLL | 
  123.                                        WS_BORDER  | WS_CHILD | 
  124.                                        WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  125.                                        0, 0, 
  126.                                        0, 0,  
  127.                                        hWnd,              
  128.                                        (HMENU)101,              
  129.                                        hInst,         
  130.                                        NULL               
  131.                                      );
  132.               }
  133.               break;
  134.  
  135.       case WM_SIZE :
  136.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  137.                                        HIWORD( lParam ), TRUE );
  138.               break; 
  139.               
  140.       case WM_COMMAND :
  141.               switch( LOWORD( wParam ) )
  142.               {
  143.                  case IDM_TEST :
  144.                         {
  145.                            BOOL  bRet;
  146.                            UCHAR szLBStr[128];
  147.                            UCHAR szBuf[BUFFERSIZE];
  148.                            WORD  cbBufOut;   // WORD matches fn prototype
  149.  
  150.                            // Call SQLGetAvailableDrivers() to 
  151.                            // retrieve a list of available drivers 
  152.                            // to install from the ODBC.INF file.
  153.                            // ....................................
  154.                            bRet = SQLGetAvailableDrivers( szIniFile,
  155.                                      szBuf, BUFFERSIZE, &cbBufOut );
  156.  
  157.                            strcpy( szLBStr, "SQLGetAvailableDrivers() " );
  158.  
  159.                            // Display status to the user.
  160.                            // ...........................
  161.                            if( bRet )
  162.                            {
  163.                               strcat( szLBStr, "successful" );
  164.  
  165.                               SendMessage( hList, LB_ADDSTRING, 0, 
  166.                                           (LPARAM)szLBStr );
  167.  
  168.                               // Display retrieved driver 
  169.                               // names to the user.
  170.                               // ........................
  171.                               DisplayBuffer( hList, szBuf );
  172.                            }
  173.                            else
  174.                            {
  175.                               strcat( szLBStr, "not successful" );
  176.  
  177.                               SendMessage( hList, LB_ADDSTRING, 0, 
  178.                                           (LPARAM)szLBStr );
  179.                            }
  180.                         }
  181.                         break;
  182.  
  183.                  case IDM_ABOUT :
  184.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  185.                         break;
  186.  
  187.                  case IDM_EXIT :
  188.                         DestroyWindow( hWnd );
  189.                         break;
  190.               }
  191.               break;
  192.       
  193.       case WM_DESTROY :
  194.               PostQuitMessage(0);
  195.               break;
  196.  
  197.       default :
  198.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  199.    }
  200.  
  201.    return( 0L );
  202. }
  203.  
  204.  
  205. // Parses the returned buffer and sends
  206. // driver names to the listbox.
  207. // ....................................
  208. VOID DisplayBuffer( HWND hList, LPCSTR lpszBuffer )
  209. {
  210.    char* p;    // string traversal pointer.
  211.  
  212.    p = (char*)lpszBuffer;
  213.  
  214.    while( *p )
  215.    {
  216.       SendMessage( hList, LB_ADDSTRING, 0, (LPARAM)p );
  217.  
  218.       // Traverse to the end of the string.
  219.       // ..................................
  220.       do
  221.       {
  222.          p++;
  223.       }
  224.       while( *p );
  225.  
  226.       // Traverse to the beginning of the next string. 
  227.       // The outer loop will terminate when we arrive
  228.       // at to the end of the buffer, since the end of 
  229.       // the buffer contains two null characters.
  230.       // .............................................
  231.       p++;
  232.    }
  233. }
  234.  
  235.  
  236. LRESULT CALLBACK About( HWND hDlg,           
  237.                         UINT message,        
  238.                         WPARAM wParam,       
  239.                         LPARAM lParam)
  240. {
  241.    switch (message) 
  242.    {
  243.        case WM_INITDIALOG: 
  244.                return (TRUE);
  245.  
  246.        case WM_COMMAND:                              
  247.                if (   LOWORD(wParam) == IDOK         
  248.                    || LOWORD(wParam) == IDCANCEL)    
  249.                {
  250.                        EndDialog(hDlg, TRUE);        
  251.                        return (TRUE);
  252.                }
  253.                break;
  254.    }
  255.  
  256.    return (FALSE); 
  257. }
  258.